#!/bin/bash
JAVA_HOME=$1
NULSTAR_URL=$3
LIBS="../../libs"
PUB_LIB=""
if [ "$4" == "address" ];
then
    MAIN_CLASS="io.nuls.cmd.client.Tools "address" $5 "
    else
    MAIN_CLASS="io.nuls.cmd.client.CmdClientBootstrap $NULSTAR_URL $5 "
fi

JAVA=${JAVA_HOME}/bin/java
#for jar in `find $LIBS -name "*.jar"`
#do
# PUB_LIB="$PUB_LIB:""$jar"
#done

APP=`ps -ef|grep -w "name=cmd-client "|grep -v grep|wc -l`
if [ $APP -eq 1 ]; then
    APP_PID=`ps -ef|grep -w "name=cmd-client "|grep -v grep|awk '{print $2}'`
    PID_EXIST=`ps -f -p $APP_PID | grep java`
    if [ ! -z "$PID_EXIST" ]; then
        echo "stopping cmd-client@${APP_PID}"
        kill ${APP_PID} > /dev/null 2>&1
    fi
fi
PUB_LIB=
if [ ! -f "./dependent.conf" ]; then
    echoRed "not found dependent config "
    exit 0
fi
while read line
do
    if [ -n "$PUB_LIB" ]; then
        PUB_LIB="${PUB_LIB}:"
    fi
    PUB_LIB="${PUB_LIB}../../libs/${line}"
done < ./dependent.conf
PUB_LIB="${PUB_LIB}:./cmd-client-1.0.0.jar"
# Get standard environment variables

JAVA_OPTS="-Xms128m -Xmx128m -Dapp.name=cmd-client --add-opens java.base/java.lang=ALL-UNNAMED --illegal-access=warn $2 "

CLASSPATH=$PUB_LIB:.
if [ -x ${JAVA} ]; then
  eval "${JAVA} $JAVA_OPTS -classpath $CLASSPATH $MAIN_CLASS"
  exit 0
fi

# $JAVA_OPTS -classpath $CLASSPATH $MAIN_CLASS

echo "The JAVA_HOME environment variable is not defined"
echo "This environment variable is needed to run this program"
exit 1